package android.slc.appbase.ui.utils;

import android.content.Context;
import android.slc.appbase.R;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.fondesa.recyclerviewdivider.DividerBuilder;
import com.fondesa.recyclerviewdivider.DividerDecoration;

/**
 * @author slc
 * @date 2021/6/24 10:45
 */
public class ItemDecorationUtils {
    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView) {
        addItemDecorationByRecyclerView(recyclerView, getDefColor(recyclerView.getContext()), getDefSize());
    }

    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView, int color, int size) {
        addItemDecorationByRecyclerView(recyclerView, color, size, true);
    }

    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView, int color, int size, boolean hintFirstDivider) {
        addItemDecorationByRecyclerView(recyclerView, color, size, hintFirstDivider, true);
    }

    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView, boolean hintFirstDivider) {
        addItemDecorationByRecyclerView(recyclerView, getDefColor(recyclerView.getContext()), getDefSize(), hintFirstDivider, true);
    }

    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView, boolean hintFirstDivider, boolean hideLastDivider) {
        addItemDecorationByRecyclerView(recyclerView, getDefColor(recyclerView.getContext()), getDefSize(), hintFirstDivider, hideLastDivider);
    }

    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView, int color, int size, boolean hintFirstDivider, boolean hideLastDivider) {
        DividerBuilder dividerBuilder = DividerDecoration.builder(recyclerView.getContext()).color(color).size(size);
        if (!hintFirstDivider) {
            dividerBuilder.showFirstDivider();
        }
        if (!hideLastDivider) {
            dividerBuilder.showLastDivider();
        }
        addItemDecorationByRecyclerView(recyclerView, dividerBuilder, false);
    }

    public static void addItemDecorationByRecyclerView(RecyclerView recyclerView, DividerBuilder dividerBuilder, boolean applyDefStyle) {
        if (applyDefStyle) {
            dividerBuilder.color(getDefColor(recyclerView.getContext())).size(getDefSize());
        }
        dividerBuilder.build().addTo(recyclerView);
    }

    public static DividerBuilder getDefStyleDividerBuilder(RecyclerView recyclerView) {
        return DividerDecoration.builder(recyclerView.getContext()).color(getDefColor(recyclerView.getContext())).size(getDefSize());
    }

    private static int getDefColor(Context context) {
        return ContextCompat.getColor(context, R.color.uiDivider);
    }

    private static int getDefSize() {
        return SizeUtils.dp2px(0.5f);
    }
}
